<template>
  <a-modal
    title="信息管理"
    :width="800"
    :visible="visible"
    :confirmLoading="loading"
    okText="确认"
    cancelText="取消"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }">
    <a-spin :spinning="loading">
      <a-form
        :form="form"
        v-bind="formLayout">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="信息名称">
              <a-input placeholder="请输入信息名称" v-decorator="['infoName',{ rules: [{ required: true, message: '请输入信息名称' }] }]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="信息内容">
              <a-input placeholder="请输入信息内容" v-decorator="['infoContent',{ rules: [{ required: true, message: '请输入信息内容' }] }]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="有效日期">
              <a-range-picker
                valueFormat="YYYY-MM-DD"
                :placeholder="['开始日期','结束日期']"
                v-decorator="['validDate',{ rules: [{ required: true, message: '请输入有效日期' }] }]"
                style="width: 280px" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="状态">
              <a-select
                placeholder="请选择状态"
                v-decorator="['status',{ rules: [{ required: true, message: '请选择状态' }] }]">
                <a-select-option :value="0">禁用
                </a-select-option>
                <a-select-option :value="1">启用
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="等级">
              <a-select
                placeholder="请选择等级"
                v-decorator="['level',{ rules: [{ required: true, message: '请选择性别' }] }]">
                <a-select-option :value="1">普通
                </a-select-option>
                <a-select-option :value="2">重要
                </a-select-option>
                <a-select-option :value="3">特别重要
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="发布人">
              <a-select
                placeholder="请选择发布人"
                v-decorator="['publisher',{ rules: [{ required: true, message: '请选择发布人' }] }]">
                <a-select-option
                  v-for="row in allUsers"
                  :key="row.userId"
                  :value="row.userId">{{ row.username }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注">
              <a-input
                placeholder="请输入备注"
                v-decorator="['note']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

// 表单字段
const fields = [
  'id',
  'infoName',
  'infoContent',
  'validDate',
  'level',
  'createDate',
  'publisher',
  'status',
  'execPerson',
  'updateTime'
]

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    allUsers: {
      type: Array,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
  }
}
</script>
